package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionSupport;

import bean.BaseResponse;
import bean.Student;
import util.BaseUtil;
import util.CRUDUtil;

public class AdminAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4911698878498224809L;
	private String message;
	private String path;
	private GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls(); 
	private Gson gson = gsonBuilder.create();

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
		
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	// 上传数据
	public void upload() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String heartrate = request.getParameter("heartrate");
		String walkstep = request.getParameter("walkstep");
		String calorie = request.getParameter("calorie");
		String eid = request.getParameter("eid");
		String date = request.getParameter("date");
		
		boolean result = CRUDUtil.insert("shouhuan", "heartrate,walkstep,calorie,eid,date", "?,?,?,?,?",
				new Object[] { heartrate, walkstep, calorie, eid,date });

		BaseResponse base = new BaseResponse(result ? 1 : 0, "", null);
		BaseUtil.returnJSONObject(request, response, gson.toJson(base));
	}

	// 登录
	public void login() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		Student student = CRUDUtil.login(username, password);

		BaseResponse base;
		if(student==null) {
			base = new BaseResponse("账号或者密码错误");
		}else {
			base = new BaseResponse(1,"登录成功",student);
		}
		BaseUtil.returnJSONObject(request, response, gson.toJson(base));

	}

}
